package service;


import dao.ActorDao;
import dao.PersonalBorrowingInformationDao;
import dao.UsersDao;
import domin.Actor;
import domin.PersonalBorrowingInformation;
import domin.Users;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;

public final class PersonalBorrowingInformationService {
    private static PersonalBorrowingInformationDao personalBorrowingInformationDao =
            PersonalBorrowingInformationDao.getInstance();

    private static PersonalBorrowingInformationService personalBorrowingInformationService

            =new PersonalBorrowingInformationService();
    private PersonalBorrowingInformationService(){

    }

    public static PersonalBorrowingInformationService getInstance(){
        return personalBorrowingInformationService;
    }

    public Collection<PersonalBorrowingInformation>
    findAll(Collection<Condition> condition, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<PersonalBorrowingInformation> personalBorrowingInformations =
                personalBorrowingInformationDao.findAll(connection,condition,pagination);
        JdbcHelper.close(connection);
        return personalBorrowingInformations;
    }

    public PersonalBorrowingInformation find(Integer id) throws SQLException {

        Connection connection = JdbcHelper.getConn();
        PersonalBorrowingInformation personalBorrowingInformation =
                personalBorrowingInformationDao.find(connection,id);
        JdbcHelper.close(connection);
        return personalBorrowingInformation;
    }
    public Collection<PersonalBorrowingInformation> getAllWithCondition(String s) throws SQLException{
        Collection<PersonalBorrowingInformation>  collection = new HashSet<PersonalBorrowingInformation>();
        Connection connection = JdbcHelper.getConn();
        Collection<PersonalBorrowingInformation> personalBorrowingInformations = PersonalBorrowingInformationDao.getInstance().getAllWithCondition(connection,s);collection.addAll(personalBorrowingInformations);
        JdbcHelper.close(connection);
        return collection;
    }

    public boolean update(PersonalBorrowingInformation personalBorrowingInformation) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        connection.setAutoCommit(false);
        try{
            updated = personalBorrowingInformationDao.update(connection,personalBorrowingInformation);
            connection.commit();
        }
        catch (SQLException e){
            connection.rollback();
        }
        finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }

        return updated;
    }

    public boolean add(PersonalBorrowingInformation personalBorrowingInformation) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean added =false;
        connection.setAutoCommit(false);
        try {
            added = personalBorrowingInformationDao.add(connection,personalBorrowingInformation);
        }
        catch (SQLException e){
            connection.rollback();
        }
        finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }

        return added;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        connection.setAutoCommit(false);
        try{
            deleted= personalBorrowingInformationDao.delete(connection,id);
        }
        catch (SQLException e){
            connection.rollback();
        }
        finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }

        return deleted;
    }
}

